package com.bankcomm.creditcard.redfriday.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpClientUtils {

	private final static String TAG = "HttpClientUtils";

	public final static String post(String url, List<BasicHeader> headers,
			List<BasicNameValuePair> body) {
		HttpPost post = new HttpPost(url);
		HttpResponse httpResponse = null;
		DefaultHttpClient client = new DefaultHttpClient();
		String result = "";
		try {
			if (body != null) {
				post.setEntity(new UrlEncodedFormEntity(body, HTTP.UTF_8));
			}
			if (headers != null) {
				for (BasicHeader basicHeader : headers) {
					post.addHeader(basicHeader);
				}
			}
			httpResponse = client.execute(post);
			if (httpResponse.getStatusLine().toString().equals("200")) {
				result = EntityUtils
						.toString(httpResponse.getEntity(), "UTF-8");
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "post: " + e.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "post: " + e.toString());
		}
		return result;
	}

	public final static byte[] post(String url,
			Map<String, String> mapRequestProperty, String body) {
		HttpURLConnection httpUrlConnection = null;
		InputStream is = null;
		OutputStream os = null;
		ByteArrayOutputStream bos = null;
		byte[] byteResponse = null;
		byte[] byteBody = null;
		try {
			byteBody = body.getBytes("UTF-8");
			httpUrlConnection = (HttpURLConnection) new URL(url)
					.openConnection();
			httpUrlConnection.setDoInput(true);
			httpUrlConnection.setDoOutput(true);
			if (mapRequestProperty != null) {
				Set<Entry<String, String>> set = mapRequestProperty.entrySet();
				for (Entry<String, String> entry : set) {
					httpUrlConnection.addRequestProperty(entry.getKey(),
							entry.getValue());
				}
			}
			httpUrlConnection.addRequestProperty("Content-Length",
					String.valueOf(byteBody.length));
			os = httpUrlConnection.getOutputStream();
			os.write(byteBody);
			os.flush();

			is = httpUrlConnection.getInputStream();
			bos = new ByteArrayOutputStream();
			int b = 0;
			while ((b = is.read()) != -1) {
				bos.write(b);
			}
			bos.flush();
			bos.toByteArray();
			byteResponse = bos.toByteArray();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "post: " + e.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "post: " + e.toString());
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Log.e(TAG, "post: " + e.toString());

				}
			}
			if (os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Log.e(TAG, "post: " + e.toString());
				}
			}
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					Log.e(TAG, "post: " + e.toString());
				}
			}
			if (httpUrlConnection != null) {
				httpUrlConnection.disconnect();
			}
		}
		return byteResponse;
	}

	public final static String get(String url, List<BasicHeader> headers,
			List<BasicNameValuePair> parameters) {
		String result = "";
		HttpResponse httpResponse = null;
		StringBuffer stb = new StringBuffer();
		if (parameters != null && !parameters.isEmpty()) {
			for (BasicNameValuePair basicNameValuePair : parameters) {
				stb.append(basicNameValuePair.getName()).append("=")
						.append(basicNameValuePair.getValue()).append("&");
			}
		}
		url = url + "?" + stb.toString().substring(0, stb.length() - 1);
		HttpGet get = new HttpGet(url);
		HttpClient client = new DefaultHttpClient();
		try {
			if (headers != null && !headers.isEmpty()) {
				for (BasicHeader basicHeader : headers) {
					get.addHeader(basicHeader);
				}
			}
			httpResponse = client.execute(get);
			if (httpResponse.getStatusLine().toString().equals("200")) {
				result = EntityUtils
						.toString(httpResponse.getEntity(), "UTF-8");
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "get: " + e.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.e(TAG, "get: " + e.toString());
		}
		return result;
	}
}
